import requests
import datetime
import re


class WeatherInfo:
    def __init__(self):
        self.api_key = "da1c1535a26e4169b66df2526706e5f5"

    def get_weather_info(self, city, days):
        url = f"https://devapi.qweather.com/v7/weather/{days}?key={self.api_key}&location={city}"
        response = requests.get(url)

        temp = re.findall(r'\d+', days)
        numbers = int(temp[0])
        # print(numbers)
        weatherData = []
        for i in range(numbers):
            if response.status_code == 200:
                data = response.json()
                if data.get('code') == '200':
                    weather_info = data['daily'][i]
                    time_str = weather_info.get('fxDate')
                    time_format = '%Y-%m-%d'
                    time = datetime.datetime.strptime(time_str, time_format)

                    current_time = time.strftime("%Y-%m-%d")
                    weather_str = f"时间：{current_time}\n" \
                                  f"天气：{weather_info['textDay']}\n" \
                                  f"最高温度：{weather_info['tempMax']}°C\n" \
                                  f"最低温度：{weather_info['tempMin']}°C\n" \
                                  f"风向：{weather_info['windDirDay']}\n" \
                                  f"风速：{weather_info['windSpeedDay']} km/h\n" \
                                  f"湿度：{weather_info['humidity']}%\n" \
                                  f"降水量：{weather_info['precip']} mm\n" \
                                  f"气压：{weather_info['pressure']} hPa\n" \
                                  f"能见度：{weather_info['vis']} km\n" \
                                  f"云量：{weather_info['cloud']}%\n" \
 \

                    weatherData.append(weather_str)
                    print(weatherData)

                else:
                    weather_str = "请给出详细的地址哦，要不然小主识别不了哦"
                    print("API请求失败")
                    return weather_str
            else:
                weather_str = "请给出详细的地址哦，要不然小主识别不了哦"
                print("请求失败")
                return weather_str
        return weatherData



# # 用户输入城市信息
# city = "116.41,39.92"
#
# # 创建WeatherInfo对象并获取天气信息
# weather_info = WeatherInfo()
# weather_info.get_weather_info(city, "7d")
